package com.xingkeduo.business.operation.controller;

import com.xingkeduo.business.operation.domain.ShopsDaysDomain;
import com.xingkeduo.pojo.json.ShopsDaysJson;
import com.xingkeduo.pojo.query.ShopsDaysQuery;
import com.xingkeduo.pojo.table.ShopsDaysTable;
import com.xingkeduo.util.tool.ReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 门店管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1 ${date}${time}
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@RestController
@RequestMapping(CMS_URL_PREFIX +"/shopsDays")
public class ShopsDaysController extends InitController {

    @Autowired
    private ShopsDaysDomain shopsDaysDomain;

    //查看
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ShopsDaysQuery shopsDaysQuery = ShopsDaysJson.createQueryForShopsDays(urlParam);
            long code = shopsDaysQuery.getCode();
            ShopsDaysTable shopsDaysTable = shopsDaysDomain.getShopsDaysByCode(code);
            return returnUtil.getSuccess(ShopsDaysJson.toJson(shopsDaysTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid ShopsDaysTable shopsDaysVo , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            shopsDaysVo.setCreatedBy(this.getOperator(request));
            shopsDaysDomain.createShopsDays(shopsDaysVo);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid ShopsDaysTable shopsDaysVo ,ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            ShopsDaysTable shopsDaysTable = ShopsDaysJson.createTableForModifyShopsDays(urlParam,shopsDaysVo);
            shopsDaysTable.setUpdatedBy(this.getOperator(request));
            shopsDaysDomain.modifyShopsDays(shopsDaysTable);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ShopsDaysQuery shopsDaysQuery = ShopsDaysJson.createQueryForShopsDays(urlParam);
            long code = shopsDaysQuery.getCode();
            shopsDaysDomain.delShopsDays(code);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }
}
